package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.TreeviewNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.entity.Area;
import cn.stylefeng.guns.sys.modular.system.mapper.AreaMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.AreaParam;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 区域表 服务实现类
 */
@Service
public class AreaService extends ServiceImpl<AreaMapper, Area> {

    @Resource
    private AreaMapper areaMapper;

    /**
     * 新增区域
     */
    @Transactional(rollbackFor = Exception.class)
    public void addArea(Area area) {

        if (ToolUtil.isOneEmpty(area, area.getSimpleName(), area.getFullName(), area.getPid(), area.getDescription())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //完善pids,根据pid拿到pid的pids
        this.areaSetPids(area);

        this.save(area);
    }

    /**
     * 修改区域
     */
    @Transactional(rollbackFor = Exception.class)
    public void editArea(Area area) {

        if (ToolUtil.isOneEmpty(area, area.getId(), area.getSimpleName(), area.getFullName(), area.getPid())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        //完善pids,根据pid拿到pid的pids
        this.areaSetPids(area);

        this.updateById(area);
    }

    /**
     * 删除区域
     */
    @Transactional
    public void deleteArea(Long areaId) {
        Area area = areaMapper.selectById(areaId);
        this.removeById(area.getId());
    }

    /**
     * 获取ztree的节点列表
     */
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    /**
     * 获取layuiTree的节点列表
     */
    public List<LayuiTreeNode> layuiTree() {
        return this.baseMapper.layuiTree();
    }

    /**
     * 获取区域名稱
     */
    public String name(Long areaId) {
        return this.baseMapper.name(areaId);
    }

    /**
     * 获取ztree的节点列表
     */
    public List<TreeviewNode> treeviewNodes() {
        return this.baseMapper.treeviewNodes();
    }

    /**
     * 获取所有区域列表
     */
    public Page<Map<String, Object>> list(String condition, Long areaId) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.list(page, condition, areaId);
    }

    /**
     * 设置区域的父级ids
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:58 PM
     */
    private void areaSetPids(Area area) {
        if (ToolUtil.isEmpty(area.getPid()) || area.getPid().equals(0L)) {
            area.setPid(0L);
            area.setPids("[0],");
        } else {
            Long pid = area.getPid();
            Area temp = this.getById(pid);
            String pids = temp.getPids();
            area.setPid(pid);
            area.setPids(pids + "[" + pid + "],");
        }
    }


    public void initAreaPids() {
        Area areaPa = new Area();
        //父级为中国的
        areaPa.setPid(1L);
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>(areaPa);
        //二级
        List<Area> areasList2 = this.areaMapper.selectList(queryWrapper);

        //二级设置pids
        for (Area area2 : areasList2) {
            String pids = new StringBuilder("[0],[").append(area2.getPid().toString()).append("],").toString();
            area2.setPids(pids);
            this.areaMapper.updateById(area2);
        }


        for (Area area2 : areasList2) {
            Area areaParam = new Area();

            areaParam.setPid(area2.getId());
            QueryWrapper<Area> queryWrapper2 = new QueryWrapper<>(areaParam);
            //三级
            List<Area> areaList3 = this.areaMapper.selectList(queryWrapper2);
            if (areaList3 != null && areaList3.size() > 0) {
                //三级设置pids
                for (Area area3 : areaList3) {
                    String pids = new StringBuilder(area2.getPids()).append("[").append(area3.getPid()).append("],").toString();
                    area3.setPids(pids);
                    this.areaMapper.updateById(area3);
                }


                //有了pids后的三级
                List<Area> pidsAreaList3 = this.areaMapper.selectList(queryWrapper2);
                for (Area area3 : pidsAreaList3) {
                    Area areaParam4 = new Area();

                    areaParam4.setPid(area3.getId());
                    QueryWrapper<Area> queryWrapper4 = new QueryWrapper<>(areaParam4);
                    //4级
                    List<Area> areaList4 = this.areaMapper.selectList(queryWrapper4);
                    if (areaList4 != null && areaList4.size() > 0) {

                        for (Area area4 : areaList4) {
                            String pids = new StringBuilder(area3.getPids()).append("[").append(area4.getPid()).append("]").toString();
                            area4.setPids(pids);
                            this.areaMapper.updateById(area4);
                        }
                    }
                }
            }
        }


        System.out.println("ssssssssssss");


    }

    public List<Area> getAreaByType(AreaParam areaParam) {
        return this.areaMapper.getAreaByType(areaParam);
    }

    public List<Area> getAreaByPid(AreaParam areaParam) {
        return this.areaMapper.getAreaByPid(areaParam);
    }
}
